var X = -1;
var isOK = false;
var ti = 120;
var daoji;
var pngno = 0;
var isClose = true;
var valid_ = true;
var code_;
var httpRequest = null;
var smallPng = ["c0_0.png", "c1_1.png", "c2_2.png", "c3_3.png", "c4_4.png",
    "c5_5.png", "c6_6.png", "c7_7.png", "c8_8.png", "c9_9.png",
    "c10_10.png", "c11_11.png", "c12_12.png", "c13_13.png", "c14_14.png"
];
var bigPng = ["c0.png", "c1.png", "c2.png", "c3.png", "c4.png",
    "c5.png", "c6.png", "c7.png", "c8.png", "c9.png",
    "c10.png", "c11.png", "c12.png", "c13.png", "c14.png"
];
var tops = [21, 23, 68, 34, 44, 56, 64, 87, 21, 47, 19, 78, 35, 21, 23];
var lefts = [174, 125, 140, 74, 76, 110, 146, 72, 118, 150, 148, 71, 185, 129, 147];

function close_() {
    var h = document.getElementsByClassName("scrollCheck")[0];
    h.style = "display:none;";
    isClose = true;
}

function change() {
    onloadPng();
}

function getCode() {
    return Math.floor(Math.random() * 900000 + 100000);
}

function onloadPng() {
    var temp = pngno;
    pngno = Math.floor(Math.random() * 15);
    while (temp == pngno) {
        pngno = Math.floor(Math.random() * 15);
    }
    var smallp = document.getElementById("_1_");
    document.getElementById("_1").src = "./res/" + bigPng[pngno];
    smallp.src = "./res/" + smallPng[pngno];
    smallp.style.top = tops[pngno] + "px";
}

//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////


function check() {
    var info = document.getElementById("info");
    var len = document.getElementById("mobile").value.length;
    if (len > 0 && len != 11) {
        info.innerHTML = "格式错误!";
        info.style = "color:#ff9911;";
    } else {
        info.innerHTML = "验证完成后,你可以使用该手机登录或找回密码";
        info.style = "color:#c5c5c5;";
        ismobileRight = 1;
    }
}

function send() {
    var len = document.getElementById("mobile").value.length;
    var info = document.getElementById("info");
    if (len == 11) {
        if (isClose) {
            onloadPng();
            document.getElementsByClassName("scrollCheck")[0].style = "display:block;";
            isClose = false;
        }
    } else {
        info.innerHTML = "格式错误!";
        info.style = "color:#ff9911;";
        document.getElementById("mobile").focus();
    }
}

function isuserOK() {
    var usr = document.getElementById("user_").value;
    var info3_ = document.getElementById("info3_");
    if (usr.length == 0) {
        info3_.innerHTML = "用户名不能为空";
        info3_.style.color = "#ff9911";
        return false;
    }
    tag = true;
    for (var i = 0; i < usr.length; i++) {
        if (usr[i] <= '/') {
            tag = false;
            break;
        }
        if (usr[i] >= ':' && usr[i] <= '@') {
            tag = false;
            break;
        }
        if (usr[i] >= '[' && usr[i] <= '^' || usr[i] == '`') {
            tag = false;
            break;
        }
        if (usr[i] >= '{' && usr[i] <= '~') {
            tag = false;
            break;
        }
    }
    if (tag == false) {
        info3_.innerHTML = "用户名不能包括非法字符";
        info3_.style.color = "#ff9911";
        return false;
    }
    info3_.style.color = "#c5c5d2";
    info3_.innerHTML = "用户名可以使用数字,下划线,字母,汉字";
    return true;
}

function ispwdOK() {
    var pwd = document.getElementById("pwd_").value;
    var info4_ = document.getElementById("info4_");
    if (pwd.length < 8) {
        info4_.innerHTML = "密码不能少于八位";
        info4_.style.color = "#ff9911";
        return false;
    }
    var tag = false;
    for (var i = 0; i < pwd.length; i++) {
        if (!(pwd[i] >= '0' && pwd[i] <= '9' || pwd[i] >= 'A' && pwd[i] <= 'Z' || pwd[i] >= 'a' && pwd[i] <= 'z' || pwd[i] == '_')) {
            info4_.innerHTML = "密码仅可以包含字母、数字、下划线";
            info4_.style.color = "#ff9911";
            return false;
        }
        if (pwd[i] >= 'A' && pwd[i] <= 'Z' || pwd[i] >= 'a' && pwd[i] <= 'z') tag = true;
    }
    if (!tag) {
        info4_.innerHTML = "密码不能为纯数字";
        info4_.style.color = "#ff9911";
        return false;
    }
    info4_.innerHTML = "密码必须包含英文字母和数字,不能少于8位,还可以使用下划线";
    info4_.style.color = "#c5c5d2";
    return true;
}

function isPwd__OK() {
    var pwd = document.getElementById("pwd_").value;
    var pwd_ = document.getElementById("pwd__").value;
    if (pwd != pwd_) {
        info4_.innerHTML = "两次密码不一致";
        info4_.style.color = "#ff9911";
        return false;
    }
    info4_.innerHTML = "密码必须包含英文字母和数字,不能少于8位,还可以使用下划线";
    info4_.style.color = "#c5c5d2";
    return true;
}

function gotoOK() {
    if (isuserOK() && ispwdOK() && isPwd__OK()) {
        var username = document.getElementById("user_").value;
        var passwd = document.getElementById("pwd_").value;
        location.href = "register_step3.jsp?" + "username=" + encodeURI(username) + "&password=" + encodeURI(passwd);
    }
}

function time_less() {
    ti--;
    document.getElementById("djs").innerHTML = ti + "秒后重新获取";
    if (ti == 0) {
        valid_ = false;
        clearTimeout(daoji);
        document.getElementById("djs").innerHTML = "重新获取";
        document.getElementById("djs").style.cursor = "pointer";
    }
}

function cxhuoqu() {
    if (ti == 0) {
        ti = 120;
        daoji = setInterval(time_less, 1000);
        code_ = getCode();
        window.open("http://localhost:8520/mid/codeSend?mobile="
            + encodeURI(document.getElementById("mobile").value) + "&code=" + encodeURI(code_));
        valid_ = true;
    }
}

function createXHR() {
//创建XMLHttpRequest对象实例
    if (window.XMLHttpRequest) {
        httpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                httpRequest = null;
            }
        }
    }
    if (!httpRequest) {
        //获取失败
        alert("获取失败");
    }
}

function sendRequest(url, params, method, handler) {
    createXHR();
    if (!httpRequest) return false;
    httpRequest.onreadystatechange = handler;
    if (method == "GET") {
        httpRequest.open(method, url + "?" + params, true);
        httpRequest.send(null);
    }
    if (method == "POST") {
        httpRequest.open(method, url, true);
        httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        httpRequest.send(params);
    }
}